#include <kapplication.h>
#include <kfiledialog.h>
#include <kdirselectdialog.h>

#include <iostream>

#include "powerbox.h"
#include "kpowerboxserver.h"


KPowerBoxServer::KPowerBoxServer()
	: QObject(kapp)
{
}


QStringList
KPowerBoxServer::showPowerbox(int operation, QString title, QString parent,
                QString startDir, QString filter, bool allowMultipleSelections)
{
	std::cout << "Parent window: " << parent.toStdString() << std::endl;

	if(allowMultipleSelections)
	{
		// TODO: get KDE to support WId for multi-select dialogs
		std::cerr
			<< "WARN: KDE doesn't support WId linking for multiple-selection"
			<< " dialogs, so we can't support multiple selection (for now)"
			<< std::endl;

		allowMultipleSelections = false;
	}


	int base = 10;
	if(parent.startsWith("0x"))
	{
		parent.replace("0x", "");
		base = 16;
	}

	WId parentWId = parent.toLong(NULL, base);
	QStringList filenames;

	switch(operation)
	{
		case OPEN_FILE:
			if(allowMultipleSelections)
				// TODO: multiple selection via WId
				filenames =
					KFileDialog::getOpenFileNames(startDir, filter, NULL, title);
			else
				filenames <<
					KFileDialog::getOpenFileNameWId(startDir, filter, parentWId, title);

			break;

		case SAVE_FILE:
			filenames <<
				KFileDialog::getSaveFileNameWId(startDir, filter, parentWId, title);
			break;

		case SELECT_DIR:
			// TODO: directory selection via WId
			filenames <<
				KFileDialog::getExistingDirectory(startDir, NULL, title);
			break;
	}

	return filenames;
}

